//
// Created by Tony on 2024/7/15.
//

#include "PUT_FIELD.h"

#include "../rt_code/Class.h"
#include "../rt_code/constant_pool/FieldRef.h"
#include <memory>
#include <stdexcept>

namespace Runtime {

using std::runtime_error;

void PUT_FIELD::execute(RtData::Frame& frame) {
    auto& curMethod = frame.getMethod();
    shared_ptr<RtCode::Class> curClass = curMethod->getOwnerClass();
    auto& curConstPool = curClass->getConstantPool();
    auto* fieldRef = curConstPool.getFieldRef(mIndex);
    auto field = fieldRef->resolvedField();
    auto slotId = field->getSlotId();

    auto* obj = frame.popRef();
    if (obj == nullptr) {
        // TODO
        throw runtime_error("tlang.NullPointerException");
    }

    auto slot = frame.popSlot();
    obj->setSlot(slotId, slot);
}

} // namespace Runtime
